/**
 * @author cmtlyt
 * @version v1.1
 */

package chapter02;

/*
 位移运算符
*/

public class DisplacementOperator {
 public static void main(String[] args) {
  int i = 21;
  System.out.printf("%d << 2 = %d\n", i, i << 2);
  System.out.printf("%d << 3 = %d\n", i, i << 3);
  // 这条会变成负数,思考一下为什么吧(什么叫做过犹不及)
  System.out.printf("%d << 27 = %d\n", i, i << 27);

  // ==============================
  int m = 12;
  int n = 5;
  System.out.printf("%d & %d = %d", m, n, m & n);
  System.out.printf("%d | %d = %d", m, n, m | n);
  System.out.printf("%d ^ %d = %d", m, n, m ^ n);

  // ======================================
  // 交换两个变量的值
  int num1 = 10;
  int num2 = 20;
  System.out.printf("num1=%d, num2=%d\n", num1, num2);

  // 方法1 推荐使用这个
  int temp = num1;
  num1 = num2;
  num2 = temp;
  System.out.printf("num1=%d, num2=%d\n", num1, num2);

  // 方法2 好处不用定义临时变量,坏处有可能会溢出,只能用于数值类型
  num1 = num1 + num2;
  num2 = num1 - num2;
  num1 = num1 - num2;
  System.out.printf("num1=%d, num2=%d\n", num1, num2);

  // 方法3 只适用于整数类型
  num1 = num1 ^ num2;
  num2 = num1 ^ num2;
  num1 = num1 ^ num2;
 }
}
